<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;

/**
 * 会员规则管理
 *
 * @icon fa fa-circle-o
 */
class Columns extends Backend
{

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Columns');
    }

    /**
     * 查看
     */
    public function index()
    {

        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model->where($where)->order($sort, $order)->count();
            $list = $this->model->where($where)->order($sort, $order)->limit($offset, $limit)->select();
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
          if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = basename(str_replace('\\', '/', get_class($this->model)));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    if(!checkRepeat('hs_apply_columns', 'column_name', $params['column_name'])){//判断字段重复
                        $result = $this->model->allowField(true)->save($params);
                    }
                    if ($result !== false) {
                        //向apply表添加字段
                        $prefix=config('database.prefix');//获取数据库前缀
                        $table=$prefix.'hs_recruit_apply';
                        $sql="alter table $table  add $params[column_name] varchar(255)  DEFAULT NULL COMMENT '$params[column_comment]'";
                        
                        $r=db()->query($sql);
                        $this->success();
                        
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row){
            $this->error(__('No Results were found'));
        }
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids)
        {
            $delIds = array_unique(explode(',', $ids));
            $count = $this->model->where('ad_id', 'in', $delIds)->delete();
            if ($count)
            {
                $this->success();
            }
        }
        $this->error();
    }

}
